var map=[[1,0,0,0,1,1,0,0,0,1],
		 [0,1,0,0,1,1,0,0,1,0],
		 [0,0,1,0,1,1,0,1,0,0],
		 [0,0,0,1,1,1,1,0,0,0],
		 [1,0,0,0,1,1,0,0,0,1]
		];
var map1=[[0,1,1,0,0,0,0,1,1,0],
		 [0,1,1,1,1,1,1,1,1,0],
		 [0,1,1,1,1,1,1,1,1,0],
		 [0,1,1,0,0,0,0,1,1,0],
		 [1,1,1,0,0,0,0,1,1,1]
		];
var map2=[[0,1,0,1,0,1,0,1,0,1],
		 [0,1,0,1,0,1,0,1,0,1],
		 [0,1,0,1,0,1,0,1,0,1],
		 [0,1,0,1,0,1,0,1,0,1],
		 [0,1,0,1,0,1,0,1,0,1]
		];
var map3=[[0,1,0,1,0,1,0,1,0,1],
		 [0,1,0,1,0,1,0,1,0,1],
		 [0,1,0,1,0,1,0,1,0,1],
		 [0,1,0,1,0,1,0,1,0,1],
		 [0,1,0,1,0,1,0,1,0,1]
		];			
var mapas = [map,map1,map2,map3];
var mapaActual = 0;		
window.addEventListener("load", start, false);

function start(){
	//START

	if(bolas[0]== null){
		canvas = document.getElementById("c");
		context = canvas.getContext("2d");
		w = canvas.width = 500;
		h = canvas.height = 500;	
		menu = new Menu();
		menu.crearMenu();
		chargeMap(mapas[mapaActual]);
		barra = new Barra();
		bolas.push(new Bola());
		controls();		
		window.setInterval(update , 1000/50);
	}
}

function restart(){
		bolas = [];
		bolas.shift();
		partidaEmpezada = false;
		puntuacion = 0;
		dificultad = 0.5;
		blocks = [];
		mapaActual = 0;	
		chargeMap(mapas[mapaActual]);		
		barra = new Barra();
		
		bolas.push(new Bola());
		items = [];	
}

function levelComplet(){
		bolas = [];
		bolas.push(new Bola());
		partidaEmpezada = false;
		blocks = [];
		if(mapaActual+1 < mapas.length){
			mapaActual++;
			chargeMap(mapas[mapaActual]);		
		}else{
			mapaActual = 0;
			chargeMap(mapas[mapaActual]);	
		}

		items = [];
}


function update(){
	if(game == 0){
		if(ENTER == 1){
			game = 1;
			ENTER = 0;
		}
	}else if(game == 1){
		menu.menuControles();
		paint(game);
	}else if(game == 2){
		if(bolas[0]== null){restart();}
		chageDificultad();
		movebolas();		
		moveBarra();
		itemsMove();
		mueveParticulas();
		mueveDisparos();
		paint(game);
	}else if(game == 10){
		window.close();
	}
}

function paint(game){
	
	var i;
	context.fillStyle = "black";
	context.fillRect(0, 0, w, h);
	for( i=0 ; i<bolas.length ; i++){
		context.drawImage(img_bola, bolas[i].x, bolas[i].y,bolas[i].w,bolas[i].h);				
	}	
	
	for( i=0 ; i<blocks.length ; i++){
		context.drawImage(img_block, blocks[i].x, blocks[i].y, blocks[i].w, blocks[i].h);				
	}
	
	context.drawImage(img_barra, barra.x, barra.y, barra.w, barra.h);
	
	for(i=0; i<items.length ; i++){
		switch(items[i].type){
			case 1:
				context.drawImage(img_fresa, items[i].x, items[i].y, items[i].w, items[i].h);
			break;
				
			case 2:
				context.drawImage(img_manzana, items[i].x, items[i].y, items[i].w, items[i].h);
			break;
			
			case 3:
				context.drawImage(img_estrella, items[i].x, items[i].y, items[i].w, items[i].h);
			break;
		}		
	}
	for(i=0;i<particulas.length;i++){
		context.drawImage(randomColor(), particulas[i].x, particulas[i].y, particulas[i].w, particulas[i].h);
	}
	
	for(i=0;i<disparos.length;i++){
		context.drawImage(img_rocket, disparos[i].x, disparos[i].y, disparos[i].w, disparos[i].h);
	}
			
	//HUD
	context.font="12px Arial";
	context.fillStyle = 'white';
	context.fillText("Mana: " + mana, 10 ,465);
	context.fillText("Puntos: " + puntuacion,10,480);
	context.fillText("Speed: " + dificultad, 10 ,492);
	
	if(QSpell==true){
		context.drawImage(icon_rocket, 100, 450, 50, 50);
	}
	if(WSpell==true){
		context.drawImage(icon_block, 170, 440, 50, 50);
	}
	
	if(ESpell == true){
		context.drawImage(icon_clock,0,0,64,64,240,440,64,64);
	}else{
		context.drawImage(icon_clock,64,0,64,64,240,440,64,64);
	}
	
	if(game==0){
		context.fillStyle = "red";
		context.fillRect(0, 150, 500, 200);		
		
		context.font="30px Arial Black";
		context.fillStyle = 'white';
		context.fillText("GAME OVER",160,260);
		context.font="17px Arial"
		context.fillText("puntos: " + puntuacion ,230,300);
		
	}else if(game == 1){
		
		context.fillStyle = "blue";
		context.fillRect(0, 100, 500, 300);	
		
		for(i=0;i< menu.enlaces.length ; i++){
			context.fillStyle ="white";
			context.font = menu.enlaces[i].font;
			context.fillText(menu.enlaces[i].returnTexto() ,
							 menu.enlaces[i].x,
							 menu.enlaces[i].y);		
		}
	}else if(game == 2){
	}
	
}

function chargeMap(map){
	var x,y, aux=[];
	//blocks = [];
	for(y=0;y<5;y++){
		for(x=0;x<10;x++){
			aux = map[y];
			if(aux[x]==1){
				blocks.push(new Block(x*50,y*25));
			}
		}
	}
	
}


function movebolas(){
	for(var i=0 ; i<bolas.length ; i++){
		bolas[i].move();
		bolas[i].moveEnBarra();
		bolas[i].colisiona();
	}
}

function mueveParticulas(){
	for(var i=0;i<particulas.length;i++){
		particulas[i].moveParticles();
	}
}

function mueveDisparos(){
	for(var i=0;i<disparos.length;i++){
		disparos[i].moveDisparos();
		disparos[i].colisionDisparos();
	}
}

function moveBarra(){
	barra.barraControles();
	barra.barraColisiones();
	barra.move();
	
}

function itemsMove(){
	for(var i=0 ; i<items.length ; i++){
		items[i].move();
	}
}	

function chageDificultad(){

	if(puntuacion < 100){
		dificultad = 0.5;
	}else if(puntuacion < 200){
		dificultad = 0.6;
	}else if(puntuacion < 300){
		dificultad = 0.7;
	}else if(puntuacion < 400){
		dificultad = 0.8;
	}else if(puntuacion < 500){
		dificultad = 0.9;
	}else if(puntuacion < 600){
		dificultad = 1;
	}else if(puntuacion < 800){
		dificultad = 1.1;
	}
}

function guardarPuntos(){

       $.post("guardarPuntos.php",{nick:prompt("NICK"), puntuacion: puntuacion},function(respuesta){
            alert(respuesta);
       });
}
	